package cn.iocoder.yudao.module.system.controller.admin.erp.customermanage;
import cn.iocoder.yudao.module.system.controller.admin.erp.customermanage.vo.CustomerManagePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.customermanage.vo.CustomerManageRespVO;
import cn.iocoder.yudao.module.system.controller.admin.erp.customermanage.vo.CustomerManageSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.erp.customermanage.CustomerManageDO;
import cn.iocoder.yudao.module.system.service.erp.customermanage.CustomerManageService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 客户管理员关系")
@RestController
@RequestMapping("/erp/customer-manage")
@Validated
public class CustomerManageController {

    @Resource
    private CustomerManageService customerManageService;

    @PostMapping("/create")
    @Operation(summary = "创建客户管理员关系")
    @PreAuthorize("@ss.hasPermission('erp:customer-manage:create')")
    public CommonResult<Long> createCustomerManage(@Valid @RequestBody CustomerManageSaveReqVO createReqVO) {
        return success(customerManageService.createCustomerManage(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户管理员关系")
    @PreAuthorize("@ss.hasPermission('erp:customer-manage:update')")
    public CommonResult<Boolean> updateCustomerManage(@Valid @RequestBody CustomerManageSaveReqVO updateReqVO) {
        customerManageService.updateCustomerManage(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户管理员关系")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:customer-manage:delete')")
    public CommonResult<Boolean> deleteCustomerManage(@RequestParam("id") Long id) {
        customerManageService.deleteCustomerManage(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户管理员关系")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:customer-manage:query')")
    public CommonResult<CustomerManageRespVO> getCustomerManage(@RequestParam("id") Long id) {
        CustomerManageDO customerManage = customerManageService.getCustomerManage(id);
        return success(BeanUtils.toBean(customerManage, CustomerManageRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户管理员关系分页")
    @PreAuthorize("@ss.hasPermission('erp:customer-manage:query')")
    public CommonResult<PageResult<CustomerManageRespVO>> getCustomerManagePage(@Valid CustomerManagePageReqVO pageReqVO) {
        PageResult<CustomerManageDO> pageResult = customerManageService.getCustomerManagePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CustomerManageRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户管理员关系 Excel")
    @PreAuthorize("@ss.hasPermission('erp:customer-manage:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCustomerManageExcel(@Valid CustomerManagePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CustomerManageDO> list = customerManageService.getCustomerManagePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户管理员关系.xls", "数据", CustomerManageRespVO.class,
                        BeanUtils.toBean(list, CustomerManageRespVO.class));
    }

}